﻿/*
 * NetSession.h
 */
#ifndef __GDK_NETSESSION_H__
#define __GDK_NETSESSION_H__

#include <gdk/core/Uuid.h>
#include <gdk/core/EventDispenser.h>
#include <gdk/core/MessageDispenser.h>
#include <gdk/core/Message.h>
#include <gdk/net/Socket.h>
#include <gdk/net/NetAddress.h>


namespace gdk
{


	//////////////////////////////////////////////////////////////////////////////
	class GDK_NET_EXPORT NetSession : virtual public Unknown
	{
		typedef Unknown::Handle_T<class NetModule>	NetModulePtr;

		DECLARE_CLASS_HANDLE(NetSession)
		DECLARE_CLASS_INSTANCE1(NetSession,class NetModule *)

		friend class NetModule;

	private:
		NetModulePtr			_netModule;
		Uuid					_clientId;
		NetAddress				_netAddress;
		Uuid					_peerSocketId;
		EventDispenser			_eventDispenser;
		MessageDispenser<void>	_messageDispenser;
		bool					_connected;
		//=========================================================================


	protected:
		NetSession(class NetModule *netModule);

	public:
		__INLINE__ class NetModule* getNetModule(void) const { return _netModule; }
		__INLINE__ const Uuid& getClientId(void) const { return _clientId; }
		__INLINE__ const NetAddress& getNetAddress(void) const { return _netAddress; }
		__INLINE__ const Uuid& getPeerSocketId(void) const { return _peerSocketId; }
		__INLINE__ EventDispenser& getEventDispenser(void) { return _eventDispenser; }
		__INLINE__ MessageDispenser<void>& getMessageDispenser(void) { return _messageDispenser; }
		//-------------------------------------------------------------------------------

	public:
		__INLINE__ bool isInvalid(void) const { return _clientId.isZero(); }
		__INLINE__ bool isConnected(void) const { return _connected; }
		//-------------------------------------------------------------------------------
		virtual bool accept(void);
		virtual bool connect(const NetAddress &addr, const Uuid &socketId = UUID_NULL);
		virtual void close(void);
		virtual bool send(Message *msg);
		//-------------------------------------------------------------------------------
		virtual bool onMessage(Message *msg);
		//-------------------------------------------------------------------------------
	};
	typedef NetSession::Handle				NetSessionPtr;
	typedef std::map<Uuid,NetSessionPtr>	NetSessionMap;
	typedef std::list<NetSessionPtr>		NetSessionList;


} //namespace gdk


#endif //__GDK_NETSESSION_H__
